﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]

    public class OrderController : ControllerBase
    {
        private readonly BitDbContext _db;
        
        public OrderController(BitDbContext db)
        {
            _db = db;
        }
        [HttpGet]
        public Result Get()
        {

            Result result = new Result();
            result.Data = _db.EM_Order.ToList();
            return result;
        }
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddOrder([FromBody] EM_Order order)
        {
            Result result = new Result();
            _db.EM_Order.Add(order);
            result.Data = _db.SaveChanges();
            return result;
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Result DelOrder(Guid Id)
        {
            Result result = new Result();
            EM_Order? order = _db.EM_Order.Where(w =>w.Id==Id).SingleOrDefault();
            if (order != null)
            {
                _db.EM_Order.Remove(order);
                result.Data = _db.SaveChanges();
            }
            return result;
            
        }
        /// <summary>
        /// 状态查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("{Id}")]
        public Result OrderStatus(Guid Id)
        {
            Result result = new Result();
            result.Data = _db.EM_Order.Where(w => w.Id == Id).ToList();
            return result;

        }
    }
}
